/*-----------------------------------------------File Info------------------------------------------------
** File Name:               uart user.h  
** Last modified date:      2016.09.8
** Last version:            V0.1
** Description:             
**
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.8
** author:                  YLY
** Version:                 V0.1
** Descriptions:            ????
**--------------------------------------------------------------------------------------------------------*/



/*   用户头文件      */
#include "userConfig.h"
#include "bspVSPI.h"

#define DELAY	50
void vDelay(uint32_t time)
{
	for(uint32_t i=0;i<time;i++)
	{
		
	}
}

void vVspiInit(void)
{
	/* GPIO INIT */
	/* Set P1 GPIO MODE */
	V_SPI_CS_HI();				//PA4
	V_SPI_CLK_HI();				//PA5
//	V_SPI_DI_HI();				//PA6
	V_SPI_DO_HI();				//PA7
}



uint8_t ucVSpiWriteReadByte(uint8_t ucByte)
{
	uint8_t i;
	uint8_t ucData;
	
	for(i=0;i<8;i++)
	{
		vDelay(DELAY);
		V_SPI_CLK_LOW();
		vDelay(DELAY);
		if(ucByte&0x80)
			V_SPI_DO_HI();
		else
			V_SPI_DO_LOW();	
		vDelay(DELAY);
		V_SPI_CLK_HI();
		vDelay(1);
		ucByte <<= 1;
		ucData <<= 1;			
		if(V_SPI_DI)
			ucData |= 0x01;
	}
	V_SPI_DO_HI();
	return ucData;
}



/*********************************************************************************************************
** Function name:       spi_SetReg
** Descriptions:        SPI写读卡芯片寄存器函数
** input parameters:    ucRegAddr：寄存器地址
**                      ucRegVal：要写入的值
** output parameters:   无
** Returned value:      TRUE
*********************************************************************************************************/
uint8_t ucVspiSetReg(uint8_t ucRegAddr, uint8_t ucRegVal)
{
	uint8_t ucVal=0;
	uint8_t i;
	
	V_SPI_CS_LOW();	
	ucVSpiWriteReadByte(ucRegAddr<<1);
	ucVSpiWriteReadByte(ucRegVal);
	V_SPI_CS_HI();
	
//	for(i=0;i<100;i++)
//		__nop();	
//	
//	V_SPI_CS_LOW();	
//	ucVSpiWriteReadByte(0x80|(ucRegAddr<<1));
//	ucVal = ucVSpiWriteReadByte(0x00);
//	V_SPI_CS_HI();
//	
//	if(ucRegVal != ucVal)
//		return false;
	return true;	
}

/*********************************************************************************************************
** Function name:       spi_GetReg
** Descriptions:        SPI读读卡芯片寄存器函数
** input parameters:    ucRegAddr：寄存器地址
** output parameters:   无
** Returned value:      目标寄存器的值
*********************************************************************************************************/
uint8_t ucVspiGetReg(uint8_t ucRegAddr)
{
    uint8_t ucRegVal;

    V_SPI_CS_LOW();	
	ucVSpiWriteReadByte(0x80|(ucRegAddr<<1));
    ucRegVal = ucVSpiWriteReadByte(0x00);
    V_SPI_CS_HI();
    return ucRegVal;
}











